﻿using AutoMapper;
using MediatR;
using MyMES.Domain.W.Liststructure;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.W.realize;

namespace MyMES.Writer.API.Application.CommendHandlers.W.realize
{
    public class UpdateMaterialmanagementCommandHanders : IRequestHandler<UpdateMaterialmanagementCommands, APIResult<Materialmanagement>>
    {
        private readonly IBaseRepository<Materialmanagement> _repository;
        private readonly IMapper mapper;

        public UpdateMaterialmanagementCommandHanders(IBaseRepository<Materialmanagement> repository, IMapper mapper)
        {
            _repository = repository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 修改物料管理信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Materialmanagement>> Handle(UpdateMaterialmanagementCommands request, CancellationToken cancellationToken)
        {
            APIResult<Materialmanagement> result=new APIResult<Materialmanagement>();
            result.Code = APIEnum.修改成功;
            result.Msg = "修改成功";
            var map = mapper.Map<Materialmanagement>(request);
            var list = $"Update Materialmanagement set Materialnumber='{request.Materialnumber}',Materialname='{request.Materialname}',Specification='{request.Specification}',Unit='{request.Unit}',Materialproduct='{request.Materialproduct}',sort='{request.sort}',IsEnable=@IsEnable,Batchmanagement=@Batchmanagement,Safetystock=@Safetystock,remark='{request.remark}',STime=@STime,IsDelete=@IsDelete where MId='{request.MId}'";
            await _repository.Update(map, list);
            return result;
        }
    }
}
